use crate::gen_title;

pub fn ch2_6_1() {
    gen_title("ch2_6_1", 18);

    let s = String::from("hello"); // s 进入作用域
    takes_ownership(s);

    let x = 5;

    makes_copy(x);
}

fn takes_ownership(some_string: String) {
    println!("{}", some_string);
}

fn makes_copy(some_integer: i32) {
    println!("{}", some_integer);
}

pub fn ch2_6_2() {
    gen_title("ch2_6_2", 18);

    let x: &str = "hello, world";
    let y = x;
    println!("{}, {}", x, y);
}

pub fn ch2_6_3() {
    gen_title("ch2_6_3", 18);

    let s1 = String::from("hello");
    let s2 = s1.clone();

    println!("s1 = {}, s2 = {}", s1, s2);
}

pub fn ch2_6_4() {
    gen_title("ch2_6_4", 18);

    let s1 = gives_ownership();

    let s2 = String::from("hello");

    let s3 = takes_and_gives_back(s2);

    println!("s1 = {}, s3 = {}", s1, s3);
}

fn gives_ownership() -> String {
    let some_string = String::from("hello");
    some_string
}

fn takes_and_gives_back(a_string: String) -> String {
    a_string
}

pub fn ch2_6_pra_1() {
    gen_title("ch2_6_pra_1", 18);

    let x = String::from("hello, world");
    let y = x.clone();
    println!("{}, {}", x, y);
}

pub fn ch2_6_pra_2() {
    gen_title("ch2_6_pra_2", 18);

    let s1 = String::from("hello, world");
    let s2 = take_ownership(s1);

    println!("{}", s2);
}

fn take_ownership(s: String) -> String {
    println!("{}", s);
    s
}

pub fn ch2_6_pra_3() {
    gen_title("ch2_6_pra_3", 18);

    let s = give_ownership();
    println!("{}", s);
}

fn give_ownership() -> String {
    let s = String::from("hello, world");
    s
}

pub fn ch2_6_pra_4() {
    gen_title("ch2_6_pra_4", 18);

    let s = String::from("hello, world");

    let s = print_str(s);

    println!("{}", s);
}

fn print_str(s: String) -> String {
    println!("{}", s);
    s
}

pub fn ch2_6_pra_5() {
    gen_title("ch2_6_pra_5", 18);

    let x = (1, 2, (), "hello");
    let y = x;
    println!("x = {:?}, y = {:?}", x, y);
}

pub fn ch2_6_pra_6() {
    gen_title("ch2_6_pra_6", 18);

    let s = String::from("hello, ");

    let mut s1 = s;

    s1.push_str("world");

    println!("{}", s1);
}

pub fn ch2_6_pra_7() {
    gen_title("ch2_6_pra_7", 18);

    let x = Box::new(5);

    let mut y = x.clone();

    *y = 4;

    assert_eq!(*x, 5);

    println!("x = {}, y = {}", x, y);
}

pub fn ch2_6_pra_8() {
    gen_title("ch2_6_pra_8", 18);

    #[derive(Debug)]
    struct Person {
        name: String,
        age: Box<u8>,
    }

    let person = Person {
        name: String::from("Alice"),
        age: Box::new(20),
    };

    let Person { name, ref age } = person;

    println!("The person's age is {}", age);
    println!("The person's name is {}", name);

    // println!("The  person struct is {:?}", person);
    println!("The person's age from person struct is {}", person.age);
}

pub fn ch2_6_pra_9() {
    gen_title("ch2_6_pra_9", 18);

    let t = (String::from("hello"), String::from("world"));

    let _s = t.0;

    println!("{:?}", t.1);
}

pub fn ch2_6_pra_10() {
    gen_title("ch2_6_pra_10", 18);

    let t = (String::from("hello"), String::from("world"));

    let (ref s1, ref s2) = t;

    println!("{:?}, {:?}, {:?}", s1, s2, t);
}
